package com.interfaces;

import java.util.List;

import javax.transaction.Transactional;

import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;

import com.entity.Terminal;

/**
 * @Title: TerminalDao.java
 * @Package com.interfaces
 * @author 孙梦楠
 * @date 2017年7月19日 下午4:49:26
 * @version V1.0.0
 */

public interface TerminalDao extends JpaRepository<Terminal, Integer> {
	
	
	//通过条件查询集中器
	@Transactional
	@Query(value = "SELECT t FROM Terminal t WHERE (t.terminalcode=?1 OR ?1 IS NULL )"
			+ "AND (t.terminalassetcode=?2 OR ?2 IS NULL )" + "AND (t.terminaltype=?3 OR ?3 IS NULL)"
			+ "AND (t.terminaladdress=?4 OR ?4 IS NULL)" + "AND (t.terminalcommuncate=?5 OR ?5 IS NULL)"
			+ "AND (t.terminalmade=?6 OR ?6 IS NULL)" + "AND (t.terminalorg=?7 OR ?7 IS NULL) "
			+ "AND (t.terminalstatus=?8 OR ?8 IS NULL) AND (t.type=?9)")
	public List<Terminal> findByConditions(String terminalcode, String terminalassetcode, String terminaltype,
			String terminaladdress, String terminalcommuncate, String terminalmade, Integer terminalorg,
			String terminalstatus,String type);
	
	List<Terminal> findByTerminalstatusNot(String terminalstatus);
	
	Terminal findByTerminalcodeAndTerminalstatusNot(String terminalcode,String terminalstatus);
	
	Terminal findByTerminalassetcodeAndTerminalstatusNot(String terminalassetcode,String terminalstatus);
	
	Terminal findByTerminaladdressAndTerminalstatusNot(String terminaladdress,String terminalstatus);
	
	//Terminalstatus不能是4
	List<Terminal> findByTerminalorgAndTypeAndTerminalstatusNot(Integer terminalorg,String type,String terminalstatus);
	
	List<Terminal> findByTypeAndTerminalstatusNot(String type,String terminalstatus);
	
	
	//根据终端地址查对象
	Terminal findByTerminaladdress(String terminaladdress);
	
	//更新集中器状态
	@Transactional
	@Modifying
	@Query("update Terminal t set t.terminalstatus = ?1 where t.terminalid = ?2") 
	int updateTerminal(String terminalstatus,Integer terminalid);
	
}
